package com.qianchang.microfinance.view.mine.bill;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineLinearLayout;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.CashLoanDetailBean;
import com.qianchang.microfinance.bean.OverListBean;
import com.qianchang.microfinance.bean.RechargeFixBean;
import com.qianchang.microfinance.bean.RechargeListItemBean;
import com.qianchang.microfinance.bean.SettleLoanBean;
import com.qianchang.microfinance.bean.SubmitLoanBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.alert.RechargeDialog;
import com.qianchang.microfinance.view.hsrecharge.HSReChargeActivity;
import com.qianchang.microfinance.view.mine.RechargeFixActivity;
import com.qianchang.microfinance.view.mine.loan.MyLoanDetailActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SettlementActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.oll_repay_total_amount)
    OneLineLinearLayout ollRepayTotalAmount;

    @BindView(R.id.oll_repay_principal)
    OneLineLinearLayout ollRepayPrincipal;

    @BindView(R.id.oll_interest)
    OneLineLinearLayout ollInterest;

    @BindView(R.id.oll_overdue_fine)
    OneLineLinearLayout ollOverdueFine;

    @BindView(R.id.oll_recharge_amount)
    OneLineLinearLayout oll_recharge_amount;


    @BindView(R.id.btn_payback)
    Button btnPayback;

    @BindView(R.id.btn_charge)
    Button btnCharge;
    private String projectId;
    private  String   url = DMConstant.Config.BASE_USER + "/loan/" + "cash" + DMConstant.Config.V + "/findSettleLoanData.do";
    private double re_money;
    private   SettleLoanBean cashLoanDetailBean;
    private List<RechargeListItemBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settlement);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_Settlement));
        btnPayback.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (null != bundle) {
            projectId = bundle.getString(ConstantAppInfo.PROJECT_ID);
        }
        getProjectDetail();
    }
    private void getProjectDetail() {
        HttpParams params = new HttpParams();
        show();
        params.put("projectId", projectId);
        DMOKHttpUtils.getInstance().post(this, url, params, new RequestCallBack<SettleLoanBean>() {
            @Override
            public void onSuccess(JsonResult<SettleLoanBean> response) {
                dismiss();
                if (response.isReqDataSuccess()) {
                     cashLoanDetailBean = response.getObject();
                    if (null != cashLoanDetailBean) {
                        refreshUi(cashLoanDetailBean);
                    }
                } else {
                    ToastUtil.getInstant().show(SettlementActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(SettlementActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }
    private void refreshUi(SettleLoanBean cashLoanDetailBean) {
        if(cashLoanDetailBean!=null){
            ollRepayPrincipal.setRightContent(FormatUtil.numKbPointFormat(cashLoanDetailBean.getPrincipal()));//本金
            ollInterest.setRightContent(FormatUtil.numKbPointFormat(cashLoanDetailBean.getInterest()));//利息
            ollOverdueFine.setRightContent(FormatUtil.numKbPointFormat(cashLoanDetailBean.getOverdue()));//违约金
            ollRepayTotalAmount.setRightContent(FormatUtil.numKbPointFormat(cashLoanDetailBean.getTotalMoney()));//总额
            oll_recharge_amount.setRightContent(FormatUtil.numKbPointFormat(cashLoanDetailBean.getRechargeMoney()));//需充值金额
           re_money= Double.parseDouble(cashLoanDetailBean.getRechargeMoney());
        }else {
            ToastUtil.getInstant().show(getApplicationContext(),getString(R.string.data_error));
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_payback:
                if(re_money==0){
                    //还款接口
                    paybackByQianpen();
                }else if(re_money>0) {
                    Bundle bundle = new Bundle();
                    bundle.putString("money",cashLoanDetailBean.getRechargeMoney());
                    bundle.putString("rechargeType","3");
                    startActivity(HSReChargeActivity.class,bundle,true);
                }
            break;

       }

    }
    private void paybackByQianpen(){
        show();
        HttpParams params = new HttpParams();
        try{
            params.put("projectId", projectId); //项目id
           params.put("period", cashLoanDetailBean.getPeriod()); //产品的哪一期
            params.put("userId", DMApplication.getInstance().getmUserLoginInfo().getUserId());
            /**
             * 订单类型：1：开通会员 2：充值金币  3：充值余额  4：手工提现 5：自动提现 6.余额正常还款 7.银行卡正常还款
             * 8.余额逾期还款 9.银行卡逾期还款 10.代还
             */
            params.put("rechargeType", "6"); // 类型 micoolWong
            params.put("principal", FormatUtil.numKbPointFormat(cashLoanDetailBean.getPrincipal())); //本金
            params.put("interest", FormatUtil.numKbPointFormat(cashLoanDetailBean.getInterest())); //利息
            params.put("overdue", FormatUtil.numKbPointFormat(cashLoanDetailBean.getOverdue())); //逾期费用

            DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.REPAY, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(JsonResult<String> response) {
                    dismiss();
                    if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                        ToastUtil.getInstant().show(SettlementActivity.this, response.getDescription());
                        finish();
                    } else{
                            ToastUtil.getInstant().show(SettlementActivity.this, response.getDescription());
                }

                }

                @Override
                public void onFailure(String msg) {
                    dismiss();
                    ToastUtil.getInstant().show(SettlementActivity.this, msg);// getString(R.string.msg_indentity_code_send_fail));
                }

                @Override
                public void onConnectFailure(String msg) {
                    ToastUtil.getInstant().show(SettlementActivity.this, msg);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
